package com.softroad.help.app.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.softroad.help.app.R;
import com.softroad.help.app.bean.ReportBean;
import com.softroad.help.app.tools.StringTool;
import com.softroad.help.app.ui.ReportListActivity.OnItemClick;

public class ReportAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private Resources resources;
	private OnItemClick onClick;
	private DisplayImageOptions options;
	private ImageLoader imageLoader;
	private List<ReportBean> data = new ArrayList<ReportBean>();

	public List<ReportBean> getData() {
		return data;
	}

	public void setData(List<ReportBean> data) {
		this.data = data;
	}

	public ReportAdapter(Context context, OnItemClick click) {
		this.inflater = LayoutInflater.from(context);
		this.resources = context.getResources();
		this.onClick = click;
		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder()
				.showImageForEmptyUri(R.drawable.defaults)
				.showImageOnFail(R.drawable.defaults).cacheInMemory(true)
				.cacheOnDisk(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return data.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(final int position, View contentView,
			ViewGroup viewGroup) {
		ViewHolder holder = null;
		if (contentView == null) {
			contentView = inflater.inflate(R.layout.adapter_report, null);
			holder = new ViewHolder();
			holder.imageView = (ImageView) contentView.findViewById(R.id.img);
			holder.textView = (TextView) contentView.findViewById(R.id.txt);
			contentView.setTag(holder);
		} else {
			holder = (ViewHolder) contentView.getTag();
		}
		final ReportBean bean = data.get(position);
		holder.textView.setText(bean.getStrTitle());
		holder.imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				onClick.onClick(position,bean);
			}
		});
		String imageUrl = bean.getImageBase64();
		if (StringTool.isNoBlankAndNoNull(imageUrl)) {
			imageLoader.displayImage(imageUrl, holder.imageView, options,
					new SimpleImageLoadingListener() {
						@Override
						public void onLoadingStarted(String imageUri, View view) {

						}

						@Override
						public void onLoadingFailed(String imageUri, View view,
								FailReason failReason) {
						}

						@Override
						public void onLoadingComplete(String imageUri,
								View view, Bitmap loadedImage) {

						}
					}, null);
		}
		return contentView;
	}

	class ViewHolder {
		private ImageView imageView;
		private TextView textView;
	}

}
